
package com.hlkj.pay.service.user.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.dto.user.UserTeleChargeOrderQueryDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.infrastructure.mapper.user.UserTeleChargeOrderMapper;
import com.hlkj.pay.infrastructure.model.user.UserTeleChargeOrderDO;
import com.hlkj.pay.service.user.IUserRechargeService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/10/25 17:32
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserRechargeServiceImpl implements IUserRechargeService {

    private final UserTeleChargeOrderMapper userTeleChargeOrderMapper;

    @Override
    public void addRechargeOrder(UserTeleChargeOrderDO teleChargeOrderDO) {
        userTeleChargeOrderMapper.insert(teleChargeOrderDO);
    }

    @Override
    public UserTeleChargeOrderDO queryRechargeOrderLast(Long userId) {
        UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto = new UserTeleChargeOrderQueryDto();
        userTeleChargeOrderQueryDto.setUserId(userId);
        LambdaQueryWrapper<UserTeleChargeOrderDO> queryWrapper = buildWrapper(userTeleChargeOrderQueryDto);
        queryWrapper.orderByDesc(UserTeleChargeOrderDO::getId);
        queryWrapper.last(" limit 1");
        return userTeleChargeOrderMapper.selectOne(queryWrapper);
    }

    @Override
    public UserTeleChargeOrderDO queryCouponRechargeOrderLast(Long userId) {
        UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto = new UserTeleChargeOrderQueryDto();
        userTeleChargeOrderQueryDto.setUserId(userId);
        LambdaQueryWrapper<UserTeleChargeOrderDO> queryWrapper = buildWrapper(userTeleChargeOrderQueryDto);
        queryWrapper.isNotNull(UserTeleChargeOrderDO::getUserCouponId);
        queryWrapper.orderByDesc(UserTeleChargeOrderDO::getId);
        queryWrapper.last(" limit 1");
        return userTeleChargeOrderMapper.selectOne(queryWrapper);
    }

    @Override
    public PageResult<UserTeleChargeOrderDO> queryPage(UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto) {
        return userTeleChargeOrderMapper.selectPage(userTeleChargeOrderQueryDto, buildWrapper(userTeleChargeOrderQueryDto));
    }

    @Override
    public UserTeleChargeOrderDO queryDetail(String chargeNo) {
        return userTeleChargeOrderMapper.selectOne(UserTeleChargeOrderDO::getChargeNo, chargeNo);
    }

    @Override
    public void updateChargeOrder(UserTeleChargeOrderDO updateUserTeleChargeOrderDO) {
        userTeleChargeOrderMapper.updateById(updateUserTeleChargeOrderDO);
    }

    /**
     * 查询条件封装
     *
     * @param userTeleChargeOrderQueryDto
     * @return
     */
    LambdaQueryWrapper<UserTeleChargeOrderDO> buildWrapper(UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto) {
        LambdaQueryWrapperX<UserTeleChargeOrderDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(UserTeleChargeOrderDO::getUserId, userTeleChargeOrderQueryDto.getUserId());
        queryWrapper.eqIfPresent(UserTeleChargeOrderDO::getUserCouponId, userTeleChargeOrderQueryDto.getUserCouponId());
        queryWrapper.eqIfPresent(UserTeleChargeOrderDO::getState, userTeleChargeOrderQueryDto.getState());
        queryWrapper.likeIfPresent(UserTeleChargeOrderDO::getChargeNo, userTeleChargeOrderQueryDto.getChargeNo());
        queryWrapper.likeIfPresent(UserTeleChargeOrderDO::getPhone, userTeleChargeOrderQueryDto.getPhone());
        queryWrapper.likeIfPresent(UserTeleChargeOrderDO::getThirdRechargeId, userTeleChargeOrderQueryDto.getThirdRechargeId());
        queryWrapper.geIfPresent(UserTeleChargeOrderDO::getCreateTime, userTeleChargeOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(UserTeleChargeOrderDO::getCreateTime, userTeleChargeOrderQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
